package com.zoo.curator.impl;

import com.jz.bean.AppInfoBean;
import com.jz.util.TimeUtil;
import com.zoo.curator.AbstractCurator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.state.ConnectionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2015/10/30.
 * 并发模式
 */
public class ConcurrentCurator extends AbstractCurator {
    private static Logger loggerChild = LoggerFactory.getLogger("child");

    public ConcurrentCurator(String execStr) {
        super(execStr, false);
        startProcess(null);
    }

    @Override
    public void startProcess(PathChildrenCacheEvent event) {
        if (exec == null) {
            LOGGER.info("exec is null");
            return;
        }
        if (child != null) {
            LOGGER.info("process已启动");
            return;
        }
        try {
            child = Runtime.getRuntime().exec(exec);
            updateAppNodeValue(new AppInfoBean(true, TimeUtil.getTime(), "CHILD start"));
            new Thread(new Runnable() {
                public void run() {
                    try {
                        LOGGER.info("开始监控child PROCESS");
                        String temp = "";
                        try {
                            BufferedReader bufferedReader = new BufferedReader(
                                    new InputStreamReader(child.getInputStream()));
                            while ((temp = bufferedReader.readLine()) != null)
                                loggerChild.info(temp);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        child.waitFor();
                        LOGGER.info("child process自行退出,置为NULL");
                    } catch (InterruptedException e) {
                        LOGGER.error("child process执行异常", e);
                        updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop error"));
                    } finally {
                        child = null;
                        updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop"));
                    }
                }
            }).start();
        } catch (Exception e) {
            LOGGER.error("child process执行异常", e);
        }
    }

    @Override
    public void killProcess(PathChildrenCacheEvent event) {
        if (child != null) {
            LOGGER.info("Killing child PROCESS");
            try {
                child.destroy();
                LOGGER.info("child process停止运行");
            } catch (Exception e) {
                LOGGER.info("child process停止运行异常");
                updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop error"));
            } finally {
                child = null;
                updateAppNodeValue(new AppInfoBean(false, TimeUtil.getTime(), "CHILD stop"));
            }
        }
    }

    @Override
    public void run() {
        try {
            while (true) {
//                Thread.sleep(100);
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void watchCurator(CuratorFramework client, CuratorEvent event) {

    }

    @Override
    public void watchConnectState(CuratorFramework client, ConnectionState state) throws Exception {

    }

    @Override
    public void watchMainNodeData(NodeCache nodeCache) {

    }

    @Override
    public void watchAppNodeData(NodeCache nodeCache) {

    }
}
